package com.minilcu.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import org.dom4j.Element;

import com.minilcu.exception.EngineException;
/**
 * @author tangyj
 * */
public class LcuEngineTool {
	/*
	 * 通过流程名子、配置文件路径，获取配置文件名子
	 * */
	public static String getFileName(String filePath,String precessName){		
		Properties propertie = new Properties();
		InputStream inputFile = LcuEngineTool.class.getClassLoader().getResourceAsStream(filePath);
		
		if(inputFile == null){
			File file = new File(filePath);
			try {
				inputFile = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				try {
					throw new EngineException("不存在配置文件："+filePath);
				} catch (EngineException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		
		try {
			propertie.load(inputFile);
			inputFile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		if(!propertie.containsKey(precessName)){
			try {
				throw new EngineException(filePath+"中没有定义流程"+precessName);
			} catch (EngineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String file = null;
		try {
			file = new String(propertie.getProperty(precessName).getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return file;
	}
	
	/*
	 * 出参入参交换数值
	 * */
	public static void swapOutIn(HashMap inParam,HashMap outParam){
		HashMap temp = (HashMap) inParam.clone();
		inParam = (HashMap)outParam.clone();
		outParam = temp;
	}
	/*
	 * java反射执行xml中配置的方法
	 * */
	public static String exeMethod(String classStr,String methodStr,Map inParam,Map outParam){
		Class<?> demo = null;
		try {
			demo = Class.forName(classStr);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		Method method = null;
		try {
			method = demo.getMethod(methodStr,inParam.getClass(),outParam.getClass());
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object obj = null;
		try {
			obj = demo.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String errorInfo = "";
		try {
			errorInfo = (String)method.invoke(obj,inParam,outParam);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return errorInfo;
	}
	/*
	 * 把配置的字符串数据信息存入入参
	 * */
	public static void bufferData(String bufferStr,Map inParam){
		String[] buffer = bufferStr.split(";");
		for(int i=0;i<buffer.length;i++){
			if(buffer[i] != null && !buffer[i].equals("")){
				String[] data = buffer[i].split("=");
				inParam.put(data[0], data[1]);
			}
		}
	}
	/*
	 * 根据上一步执行中的outParam判断程序走向
	 * */
	public static String decision(List<Element> eleList,Map inParam){
		String nextEle = null;
		Set set = inParam.keySet();
		Iterator inIt = set.iterator();
		JexlContext jexlContext = new MapContext();
		while(inIt.hasNext()){
			Object key = inIt.next();
			jexlContext.set(key.toString(),inParam.get(key));
		}
		
		Iterator eleIt = eleList.iterator();
		while(eleIt.hasNext()){
			Element ele = (Element)eleIt.next();
			String expression = ele.getTextTrim();
			if(expression !=null && !expression.equals("")){
				Expression e = (Expression) new JexlEngine().createExpression(expression);
		        Boolean result = (Boolean) e.evaluate(jexlContext);
		        if(result){
		        	nextEle = ele.attributeValue("to");
		        	break;
		        }
			}
		}
		if(nextEle == null){
			Element lastEle = eleList.get(eleList.size()-1);
			nextEle = lastEle.attributeValue("to");
		}
		return nextEle;
	}
}
